﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace LD.Host
{
    public partial class ToolItem : UserControl
    {
        public ToolItem()
        {
            InitializeComponent();
            this.lab_ExecuteTime.Text = "";
            this.lab_name.Text = "";
            this.lab_alias.Text = "";
            this.pic_result.Visible = false;
        }
        public ToolItem(TB_Tool tool) : this()
        {
            Tool = tool;
            this.lab_alias.Text = tool.Text;
            this.lab_name.Text = tool.Name;
            this.IsEnabled = tool.Enabled;
        }
        private TB_Tool tool;
        public TB_Tool Tool
        {
            get { return tool; }
            set
            {
                tool = value;
                this.lab_alias.Text = tool.Text;
                this.lab_name.Text = tool.Name;
            }
        }

        private long executeTime = 0;
        public long ExecuteTime
        {
            get { return executeTime; }
            set
            {
                executeTime = value;
                this.lab_ExecuteTime.Text = value + "ms";
                this.pic_result.Visible = false;
            }
        }
        public OpResult Execute()
        {
            OpResult opResult = FlowHelper.Execute(Tool);// ToolTypeCollection.Tools[ToolType].Execute(ParamData);
            ExecuteTime = opResult.ExecuteTime;
            this.pic_result.Visible = true;
            if (opResult.IsSuccess)
                this.pic_result.Image = Properties.Resources.right;
            else
            {
                this.pic_result.Image = Properties.Resources.error;

                //单步运行不在执行失败动作
                //失败后执行的动作
                //if (Tool.FailSkip)
                //    FlowHelper.RunFlow(Tool.FlowName);
            }

            return opResult;
        }
        private bool isEnabled = true;
        public bool IsEnabled
        {
            get { return isEnabled; }

            set
            {
                isEnabled = value;
                Tool.Enabled = value;
                if (isEnabled)
                    this.txt_top.BackColor = Color.FromArgb(27, 91, 158);
                else
                    this.txt_top.BackColor = Color.Red;
            }
        }
    }
}
